package xhwl.insurance.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import java.util.ArrayList;

import xhwl.android.common.util.StringUtil;
import xhwl.insurance.R;
import xhwl.insurance.http.bean.resp.MessageCenterResp;

/**
 * Created by admin on 2017-09-04.
 */
public class ListViewSlideAdapter extends BaseAdapter{

    private ArrayList<MessageCenterResp> mList;
    private Context context;
    private OnClickListenerEditOrDelete onClickListenerEditOrDelete;

    public ListViewSlideAdapter(Context context, ArrayList<MessageCenterResp> list){
        this.mList=list;
        this.context=context;
    }

    @Override
    public int getCount() {
        return mList.size();
    }

    @Override
    public Object getItem(int position) {
        return mList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        final MessageCenterResp resp=mList.get(position);
        ViewHolder viewHolder;
        if(null == convertView) {
            convertView = View.inflate(context, R.layout.item_slide_delete_edit, null);
            viewHolder=new ViewHolder();
            viewHolder.mTitle = (TextView) convertView.findViewById(R.id.tv_message_title);
            viewHolder.mContent = (TextView) convertView.findViewById(R.id.tv_message_content);
            viewHolder.mDate= (TextView) convertView.findViewById(R.id.tv_message_date);
            viewHolder.tvDelete=(TextView)convertView.findViewById(R.id.delete);
            convertView.setTag(viewHolder);//store up viewHolder
        }else {
            viewHolder=(ViewHolder)convertView.getTag();
        }

        String title=mList.get(position).getPUSH_TITLE();
        if(StringUtil.isEmpty(title)){
            title="通知";
        }
        viewHolder.mTitle.setText(title);
        viewHolder.mContent.setText(resp.getPUSH_CONTENT());
        viewHolder.mDate.setText(resp.getCREATE_DATE());
        viewHolder.tvDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onClickListenerEditOrDelete!=null){
                    onClickListenerEditOrDelete.OnClickListenerDelete(position);
                }
            }
        });

        return convertView;
    }

    private class ViewHolder{
        private TextView tvDelete;
        private TextView mDate;
        private TextView mTitle;
        private TextView mContent;
    }

    public interface OnClickListenerEditOrDelete{
        void OnClickListenerDelete(int position);
    }

    public void setOnClickListenerEditOrDelete(OnClickListenerEditOrDelete onClickListenerEditOrDelete1){
        this.onClickListenerEditOrDelete=onClickListenerEditOrDelete1;
    }

}

